use_frameworks!
target 'HDCoverageDemo' do
  pod 'HDCoverage', :path => '../'
  pod 'AFNetworking'
  
end

# 如果失效，可以使用 pod install --clean-install
# 实现post_install Hooks
# 需要收集Code Coverage的模块
ntargets = Array['AFNetworking']

require 'xcodeproj'
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if ((config.name <=> 'Release') == 0 || (config.name <=> 'Debug') == 0)
        config.build_settings['OTHER_CFLAGS'] = '$(inherited)'
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited)'
        config.build_settings['OTHER_LDFLAGS'] = '$(inherited)'
        ntargets.each do |ntarget|
          if(ntarget <=> target.name) == 0
            config.build_settings['OTHER_CFLAGS'] = '$(inherited) -fprofile-instr-generate -fcoverage-mapping'
            config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -profile-generate -profile-coverage-mapping'
            config.build_settings['OTHER_LDFLAGS'] = '$(inherited) -fprofile-instr-generate'
            break
          end
        end
        else
        config.build_settings['OTHER_CFLAGS'] = '$(inherited)'
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited)'
        config.build_settings['OTHER_LDFLAGS'] = '$(inherited)'
      end
    end
  end
  
  # 修改主工程
  project_path = './HDCoverage.xcodeproj'
  project = Xcodeproj::Project.open(project_path)
  puts project
  project.targets.each do |target|
    if(target.name <=> 'HDCoverageDemo') == 0
      target.build_configurations.each do |config|
        if ((config.name <=> 'Release') == 0 || (config.name <=> 'Debug') == 0)
          # 设置预编译变量CODECOVERAGE
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = '$(inherited) CODECOVERAGE=1'
          # OC代码覆盖率插桩配置
          config.build_settings['OTHER_CFLAGS'] = '$(inherited) -fprofile-instr-generate -fcoverage-mapping'
          # Swift代码覆盖率插桩配置
          config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -profile-generate -profile-coverage-mapping'
          # 采集代码覆盖率配置
          config.build_settings['OTHER_LDFLAGS'] = '$(inherited) -fprofile-instr-generate'
          # Release需要设置，不然无法解析代码覆盖率
          config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
          else
          config.build_settings['OTHER_CFLAGS'] = '$(inherited)'
          config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited)'
          config.build_settings['OTHER_LDFLAGS'] = '$(inherited)'
          config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = ''
        end
      end
        #第一个参数,相对于.xcodeproj 项目根目录,一定要和工程里的根目录名字相同
        #第二个参数,相对工程里的目录
        mapiGroup = project.main_group.find_subpath(File.join('../HDCoverage','Classes'), true)
        #创建工程目录,没有可以创建
        mapiGroup.set_source_tree('<group>')
        #源,相对于.xcodeproj路径,提供给项目实体文件的路径
        mapiGroup.set_path("Classes")


        #移除索引
        def removeBuildPhaseFilesRecursively(aTarget, aGroup)
            aGroup.files.each do |file|
                aTarget.source_build_phase.remove_file_reference(file)
            end

            aGroup.groups.each do |group|
                removeBuildPhaseFilesRecursively(aTarget, group)
            end
        end

        #添加索引
        def addFilesToGroup(aTarget, aGroup)
            Dir.foreach(aGroup.real_path) do |entry|
                filePath = File.join(aGroup.real_path, entry)
                # 过滤目录和.DS_Store文件
                if entry != ".DS_Store" && !filePath.to_s.end_with?(".meta") &&entry != "." &&entry != ".."then
                    # 向group中增加文件引用
                    fileReference = aGroup.new_reference(filePath)
                    aTarget.source_build_phase.add_file_reference(fileReference, true)
                    if filePath.to_s.end_with?(".h") then
                        build_file=aTarget.source_build_phase.build_file(fileReference)
                        aTarget.source_build_phase.remove_build_file(build_file)
                    end
                end
            end
        end

        if !mapiGroup.empty? then
            removeBuildPhaseFilesRecursively(target,mapiGroup)
            mapiGroup.clear()
        end

        addFilesToGroup(target, mapiGroup)
        puts "重组目录结构..."
    end
  end
  project.save()
end
